package com.uam.web.controller;

import com.uam.common.annotation.Log;
import com.uam.service.RoleService;
import com.uam.web.common.model.BusinessType;
import com.uam.web.common.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author uam
 * @create 2023/11/30 20:19
 * @email uam@uam.com
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Log(title = "查询所有角色", businessType = BusinessType.QUERY_LOG)
    @ApiOperation(value = "获取所有角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
    })
    @GetMapping("/get")
    public Result getRole() {
        return roleService.getRole();
    }

    @Log(title = "查询角色菜单", businessType = BusinessType.QUERY_LOG)
    @ApiOperation(value = "获取角色对应菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "roleName", value = "用户角色:不传则获取所有菜单",
                    example = "例：管理员、教师、学生", dataTypeClass = String.class),
    })
    @PostMapping("/getMenu")
    public Result getMenuByRole(@RequestParam(required = false) String roleName) {
        return roleService.getMenuByRole(roleName);
    }

    @Log(title = "编辑权限")
    @ApiOperation(value = "配置权限菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "roleName", value = "用户角色",
                    example = "例：管理员、教师、学生", dataTypeClass = String.class),
            @ApiImplicitParam(name = "menuIds", value = "菜单id",
                    example = "12", dataTypeClass = Long.class),
    })
    @PostMapping("/setMenu")
    public Result setRoleMenu(@RequestParam(required = false) String roleName,
                              @RequestParam(required = false) List<Long> menuIds) {
        return roleService.setRoleMenu(roleName, menuIds);
    }

    @Log(title = "新增角色")
    @ApiOperation(value = "新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "header"),
            @ApiImplicitParam(name = "roleName", value = "用户角色:不传则获取所有菜单",
                    example = "例：管理员、教师、学生", dataTypeClass = String.class),
    })
    @PreAuthorize("@customExpression.hasAuthority('暂时关闭')")
    @PostMapping("/add")
    public Result addRole(@RequestParam(required = false) String roleName) {
        return roleService.addRole(roleName);
    }
}
